package com.gm.wholesale.module.custom.view;
import android.content.Context;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.style.ForegroundColorSpan;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import androidx.annotation.Nullable;

import com.gm.commonlib.module.zxing.QRCodeUtil;
import com.gm.commonlib.util.logUtils.LogUtil;
import com.gm.commonlib.views.popupwindow.Popupwindow;
import com.gm.wholesale.R;
import com.gm.wholesale.module.wx.bean.ShareHtmlBean;
public class ShareView extends LinearLayout {

    private TextView titleTv;
    private TextView cancleTv;
    private TextView sharTv;
    private ImageView imageView;

    public ShareView(Context context) {
        super(context);
        initView(context);
    }

    public ShareView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initView(context);
    }

    private void initView(Context context){
        LayoutInflater.from(context).inflate(R.layout.view_share_invint,this);
        titleTv = findViewById(R.id.titleTv);
        cancleTv = findViewById(R.id.cancleTv);
        imageView = findViewById(R.id.imageView);
        sharTv = findViewById(R.id.sharTv);
        cancleTv.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                Popupwindow.hiddenPop();
            }
        });

    }

    public void show(ShareHtmlBean shareHtmlBean,OnClickListener listener){
        if (shareHtmlBean==null)return;
        LogUtil.d("分享信息: "+shareHtmlBean.toString());
        imageView.setImageBitmap(QRCodeUtil.createQRCode(shareHtmlBean.getHtmlUrl(),300));
        String title=shareHtmlBean.getTitle();
        SpannableStringBuilder builder = new SpannableStringBuilder(title);
        ForegroundColorSpan colorSpan = new ForegroundColorSpan(getResources().getColor(R.color.color_06C160));
        int start = title.indexOf("\"");
        int end = title.lastIndexOf("\"");
        builder.setSpan(colorSpan, start+1, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        titleTv.setText(builder);
        sharTv.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                Popupwindow.hiddenPop();
                listener.onClick(v);
            }
        });
    }
}
